<?php
namespace backend\controllers;

use Yii;
use yii\helpers\Html;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use backend\models\Contact;
use common\models\User;
use yii\web\Response;
use common\helpers\StringHelper;

class ContactController extends Controller
{
    public $enableCsrfValidation = false;

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index','view'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }
    public function actions(){
        return [
            'ueditor'=>[
                'class' => 'common\widgets\ueditor\UeditorAction',
                'config'=>[
                    //上传图片配置
                    'imageUrlPrefix' => "http://image.bbeats.io/", /* 图片访问路径前缀 */
                    'imagePathFormat' => "uploads/news/", /* 上传保存路径,可以自定义保存路径和文件名格式 */
                ]
            ],
            'crop'=>[
                'class' => 'common\widgets\avatar\CropAction',
                'config'=>[
                    'bigImageWidth' => '2500',     //大图默认宽度
                    'bigImageHeight' => '1500',    //大图默认高度
                    'middleImageWidth'=> '400',   //中图默认宽度
                    'middleImageHeight'=> '220',  //中图图默认高度
                    'smallImageWidth' => '50',    //小图默认宽度
                    'smallImageHeight' => '50',   //小图默认高度
                    //头像上传目录（注：目录前不能加"/"）
                    'uploadPath' => 'uploads/news',
                ]
            ]
        ];
    }

    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Contact::find()->orderBy("ctime desc"),
        ]);
        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    public function actionDelete($id)
    {
        Contact::updateAll(['status' => 1],['id' => $id]);
        return $this->redirect(['index']);
    }

    /**
     * 返回格式参见
     */
    public function actionUpload()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        //TODO FileUpload
    }

    protected function findModel($id)
    {
        if (($model = Contact::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('您请求的页面不存在');
        }
    }
}
